//
//  BannersParser.m
//  InTheSnow
//
//  Created by di on 7/29/09.
//  Copyright 2009 INTELLECTSOFT. All rights reserved.
//
#import "UIBannerImageView.h"
#import "BannersParser.h"


@implementation BannersParser

@synthesize isUrl;
@synthesize animationImages;
@synthesize currentBanner;

- (id) initWithUrl:(NSURL*)url
{
	self = [super init];
	if (self != nil) {
		self.isUrl = url;
	}
	return self;
}


- (id)initWithContentsOfURL:(NSURL *)ur{
	self = [super init];
	if (self != nil) { 
		
		[[NSURLCache sharedURLCache] setMemoryCapacity:0];
		[[NSURLCache sharedURLCache] setDiskCapacity:0];
 
		self.animationImages = [NSMutableArray array];
			
		NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:ur];
		[parser setDelegate:self]; 
		[parser setShouldProcessNamespaces:NO];
		[parser setShouldReportNamespacePrefixes:NO];
		[parser setShouldResolveExternalEntities:YES];		
		[parser parse];		
		[parser release];
		
	}
	return self;
}

- (void) parseData:(NSData*)dt
{
	[[NSURLCache sharedURLCache] setMemoryCapacity:0];
	[[NSURLCache sharedURLCache] setDiskCapacity:0];
	self.animationImages = [NSMutableArray array];
	
	NSXMLParser *parser = [[NSXMLParser alloc] initWithData:dt];
	[parser setDelegate:self]; 
	[parser setShouldProcessNamespaces:NO];
	[parser setShouldReportNamespacePrefixes:NO];
	[parser setShouldResolveExternalEntities:YES];		
	[parser parse];		
	[parser release];
//	NSLog(@"banneer parser data");
	
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	if (qName)
	{
		elementName = qName;
	}	 
//	NSLog(elementName);
	if([elementName isEqualToString:@"banner"])
	{ 
		self.currentBanner = [[UIBannerImageView alloc] initWithURL:[NSURL URLWithString:[attributeDict objectForKey:@"link"]]];
		NSLog([attributeDict objectForKey:@"link"]);
		currentBanner.timeout = (NSInteger)[[attributeDict objectForKey:@"timeout"] intValue];
		[__delegate.banners addObject:self.currentBanner];
		
		
		return;
	}
	
	if([elementName isEqualToString:@"png"])
	{ 
		NSString *url = [(NSString*)[attributeDict objectForKey:@"url"] stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
		UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
		if(img != nil)[self.animationImages addObject:img];
		else{
			[self.animationImages addObject:[NSNull null]];	
			NSLog(@"png null");
		} 
		
		[img release];
//		NSLog(@"banner img: %@",[attributeDict objectForKey:@"png"]);
		return;
	}
	if([elementName isEqualToString:@"description"]){  
		return;
	}
	
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
	NSLog(@" ERROR  %i - %i", [parser lineNumber], [parser columnNumber]);
	NSLog(@" %@, user info %@", [parseError localizedFailureReason], [parseError localizedDescription]);

	return;
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
	if (qName)
	{
		elementName = qName;
	}
	if([elementName isEqualToString:@"banner"])
	{ 
		[currentBanner setAnimationImages:self.animationImages];
		[self.animationImages removeAllObjects];
		return;
	}
}

- (void)parserDidEndDocument:(NSXMLParser *)parser{
 
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
}



- (void) dealloc
{

	[isUrl release];
	[animationImages release];
	[currentBanner release];
	[super dealloc];
}

@end
